$("#addBtn").click(
    function () {
        var tpreg = /^[0-9a-zA-Z]+$/;
        var tireg = /^[\u2E80-\u9FFF]+$/;
        console.log("111");
        var typeInstruct = $("#typeInstruct").val();
        var typeName = $("#typeName").val();
        var emTypeId = $("#emTypeId").val();
        if ((tpreg.test(typeName)) && (tireg.test(typeInstruct))) {
            var formData = new FormData();
            formData.append('typeInstruct', typeInstruct);
            formData.append('typeName', typeName);
            var url = "/u/atype";
            //如果主键有值，则进行更新，否则进行添加
            if (emTypeId !== null && emTypeId !== undefined && emTypeId !== '') {
                formData.append('emTypeId', emTypeId);
            }
            $.ajax({
                url: url,
                type: 'POST',
                cache: false,
                data: formData,
                processData: false,
                contentType: false
            }).done(function (res) {
                if (res === null || res === undefined || res === '') {
                    alert("服务器错误");
                    return;
                }
                console.log(res);
                alert(res.data);
                $("#typeInstruct").val("");
                $("#typeName").val("");
                $("#emTypeId").val("");
                 window.location.reload();
                //  return res;
            }).fail(function (res) {
                console.log(res);
                alert("网络错误");

                //  return res;
            });

        } else {
            alert("请检查输入的数据");
        }
    });

function updateState(emTypeId, state) {
    var formData = new FormData();
    formData.append('emTypeId', emTypeId);
    formData.append('state', state);
    var url = "/u/atype";
    $.ajax({
        url: url,
        type: 'POST',
        cache: false,
        data: formData,
        processData: false,
        contentType: false
    }).done(function (res) {
        if (res === null || res === undefined || res === '') {
            alert("服务器错误");
            return;
        }
        alert(res.data);
        window.location.reload();
        //  return res;
    }).fail(function (res) {
        console.log(res);
        alert("网络错误");
    });
}

function delType(emTypeId) {
    var modal = $('#my-confirm');
    modal.modal({
        'width': '600',
        onConfirm: function () {
            var formData = new FormData();
            formData.append('emTypeId', emTypeId);
            var url = "/u/del";
            $.ajax({
                url: url,
                type: 'POST',
                cache: false,
                data: formData,
                processData: false,
                contentType: false
            }).done(function (res) {
                if (res === null || res === undefined || res === '') {
                    alert("服务器错误,请检查数据是否完整");
                    return;
                }
                alert(res.data);
                window.location.reload();
                //  return res;
            }).fail(function (res) {
                console.log(res);
                alert("网络错误，请检查数据是否完整");
            });

        }
    });
}




function editType(emTypeId) {
    var modal = $('#type-model');
    modal.modal({target: '#type-model', width: 600});
    var formData = new FormData();
    formData.append('emTypeId', emTypeId);
    var url = "/u/one";
    $.ajax({
        url: url,
        type: 'POST',
        cache: false,
        data: formData,
        processData: false,
        contentType: false
    }).done(function (res) {
        if (res === null || res === undefined || res === '') {
            alert("服务器错误");
            return;
        }
        console.log(res);
        $("#typeInstruct").val(res.typeInstruct);
        $("#typeName").val(res.typeName);
        $("#emTypeId").val(res.emTypeId);
    }).fail(function (res) {
        console.log(res);
        alert("网络异常")

    });

}

$("#typeName").change(function () {
    var reg = /^[0-9a-zA-Z]+$/;
    var typeName = $("#typeName").val();
    if (!reg.test(typeName)) {
        $("#typeName").css("borderStyle", "solid");
        $("#typeName").css("borderColor", "red");
        $("#typeName").css("borderWidth", "1px");
        $("#typeName").focus();
        // $("#nameMsg").text("必须输入字母");
        // alert("必须输入字母")
    } else {
        $("#typeName").css("borderStyle", "");
        $("#typeName").css("borderColor", "");
        $("#typeName").css("borderWidth", "");
        // $("#nameMsg").text("*");
    }
});
$("#typeInstruct").change(function () {
    var reg = /^[\u2E80-\u9FFF]+$/;
    ;
    var typeInstruct = $("#typeInstruct").val();
    if (!reg.test(typeInstruct)) {
        $("#typeInstruct").css("borderStyle", "solid");
        $("#typeInstruct").css("borderColor", "red");
        $("#typeInstruct").css("borderWidth", "1px");
        $("#typeInstruct").focus();
    } else {
        $("#typeInstruct").css("borderStyle", "");
        $("#typeInstruct").css("borderColor", "");
        $("#typeInstruct").css("borderWidth", "");
    }
});